import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const baseUrl = CommonConfig.machinestatusUrl + '/v1'
const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
 * 启用/禁用状态
 * param enable id
 * @returns {Promise<unknown>} promise
 */
export function changeCauseEnable (query) {
  const url = baseUrl + '/stop_cause_item_enable'

  const params = {
    enable: query.enable,
    ids: typeof query.id !== 'object' ? [query.id] : query.id
  }
  return axiosInstance.patch(url, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 查询所有原因组
 * 机台报表查询停机原因时传入false,不需要过滤
 */
export function getCauseTypeList (causeType, isFilter) {
  const reqData = {
    params: {
      causeType,
      isFilter: !isFilter
    }
  }
  const url = baseUrl + '/stop_cause_type:selectBatch'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除原因
 */
export function delCause (query) {
  const reqData = {
    params: {
      ids: query.id || undefined
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = baseUrl + '/stop_cause_item_batchDel'
  return axiosInstance.delete(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
// 编辑原因
export function editCauseItem (query) {
  const params = {
    code: query.typeCode || undefined,
    id: query.id || undefined,
    label: query.typeName || undefined,
    gid: query.gid || undefined
  }
  const url = baseUrl + '/stop_cause_item:edit'
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 编辑原因类型
export function editCauseType (query) {
  const params = {
    label: query.label || undefined,
    id: query.code || undefined,
    causeType: query.causeType || undefined
  }
  const url = baseUrl + '/stop_cause_type:edit'
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除原因类型
 */

export function delCauseType (query) {
  const config = {
    params: {
      id: query.id || undefined,
      causeType: query.causeType || undefined
    }
  }
  const url = baseUrl + '/stop_cause_type:delete'
  return axiosInstance.delete(url, config).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 根据原因组代码查询原因
 */
export function getCauseItem (query, { pageNum, pageSize }) {
  const reqData = {
    params: {
      causeGroupId: query.id || undefined,
      showDisable: query.showDisable,
      pageNum,
      pageSize
    }
  }
  const url = baseUrl + '/stop_cause_item:get'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 增加原因
 */

export function addCauseItem (query) {
  const config = {
    params: query,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = baseUrl + '/stop_cause_item:add'
  return axiosInstance.post(url, config.params).then((res) => {
    return Promise.resolve((res))
  })
}
// 新增原因类型
export function addCauseType (query) {
  const params = {
    label: query.label || undefined,
    causeType: query.causeType || undefined
  }
  const url = baseUrl + '/stop_cause_type:add'
  return axiosInstance.post(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 获取停机设置
export function getSelectTenantList (tenantId) {
  const url = baseUrl + `/stop_custom/${tenantId}/select_tenant`
  return axiosInstance.get(url).then((res) => {
    return Promise.resolve((res))
  })
}

// /v1/stop_custom:update
// 修改自定义停机
export function putStopCustom (params) {
  const url = baseUrl + '/stop_custom:update'
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/stop_custom:add
// 添加自定义停机
export function postStopCustom (params) {
  const url = baseUrl + '/stop_custom:add'
  return axiosInstance.post(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// DELETE
// /v1/stop_custom:delete
//   通过id删除自定义停机
export function delStopCustom (id) {
  const url = baseUrl + `/stop_custom:delete?id=${id}`
  return axiosInstance.delete(url).then((res) => {
    return Promise.resolve((res))
  })
}
